package TUILayer;
import java.util.Scanner;
import java.util.ArrayList;
import ControlLayer.DVDCtr;
import ModelLayer.DVD;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller
    boolean returnTo;
    
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
    }
    
    public void start()
    {
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
        //show menu and return the user's choice
        int choice =   writeDVDMenu();
        if(choice == 1)
        {
            // Read DVD
            String title = inputTitle();
            DVD d = dvdCtr.findDVD(title);
            System.out.print("\f");
            System.out.println("Title        : " + d.getTitle());
            System.out.println("Publisher    : " + d.getPublisher());
            System.out.println("Release Year : " + d.getReleaseYear());
            int noOfCopies = d.getNoOfCopies();
            if(noOfCopies > 0)
            {
                System.out.println("Copies       : " + noOfCopies);
            }
            else
            {
                System.out.println("No copies added yet.");
            }
            
            // Pause console loop to allow user to read output before returning to menus.
            returnTo = false;
            while(!returnTo)
            {
                pressAnyKey();
            }
        }
        else
        {
            if(choice == 2)
            {
                // Create DVD
                String title = inputTitle();
                String publisher = inputPublisher();
                String releaseYear = inputReleaseYear();
                
                dvdCtr.createDVD(title, publisher, releaseYear);
            }
            else
            {
                if(choice == 3)
                {
                    // Create Copy of DVD
                    // Find the DVD to have a copy added to
                    String title = inputTitle();
                    DVD d = dvdCtr.findDVD(title);
                    
                    //Get input for Copy and send it to DVD
                    String serialNo = inputSerialNo();
                    int purchaseDate = purchaseDate();
                    double purchasePrice = purchasePrice();
                    
                    d.createCopy(serialNo, purchaseDate, purchasePrice);
                }
                else
                {
                if(choice == 4)
                {
                    // Delete DVD
                    String title = inputTitle();
                    dvdCtr.deleteDVD(title);
                }
                else
                {
                if(choice == 5)
                {
                    // listAllDVDs
                    ArrayList<DVD> d = dvdCtr.listAllDvds();
                    System.out.println("\f*** DVDs owned ***");
                    for(DVD DVD : d)
                    {
                        System.out.println("\n");
                        System.out.println("Title           :" + DVD.getTitle());
                        System.out.println("Publisher       :" + DVD.getPublisher());
                        System.out.println("Year of release : " + DVD. getReleaseYear());
                    }
                }
                else
                {
                exit = true;
                }//end else
                }//end else
                }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Add DVD to collection");
            System.out.println(" (3) Add copy of DVD to collection");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    public void pressAnyKey()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("\nPress any key to continue:  ");
         keyboard.nextLine();
         returnTo = true;
    }
      
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    private String inputPublisher()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type publisher: " );
        String publisher = keyboard.nextLine();
        return publisher;
    }
    private String inputReleaseYear()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type year of release: " );
        String releaseYear = keyboard.nextLine();
        return releaseYear;
    }
    private String inputSerialNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type serial number: ");
        String serialNo = keyboard.nextLine();
        return serialNo;
    }
    private int purchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type purchase date: ");
        int purchaseDate = keyboard.nextInt();
        return purchaseDate;
    }
    private double purchasePrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type purchase price: ");
        double purchasePrice = keyboard.nextDouble();
        return purchasePrice;
    }
    
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        int no = keyboard.nextInt();
        return no;
    }
}